package org.ishequ.service;

import java.util.ArrayList;
import java.util.List;
import org.ishequ.domain.MyContacts;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;

public class MyContactsService {
	
	private Context context;
	
	public MyContactsService(Context context){
		this.context = context;
	}
	
	//带事务的添加
	public void saveContacts2(MyContacts contacts) throws RemoteException, OperationApplicationException{
		ContentResolver contentResolver = context.getContentResolver();
		ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
		
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentProviderOperation op1 = 
			ContentProviderOperation.newInsert(uri).withValue("account_name", null)
			.build();
		operations.add(op1);
		
		uri = Uri.parse("content://com.android.contacts/data");
		ContentProviderOperation op2 = 
			ContentProviderOperation.newInsert(uri).withValueBackReference("raw_contact_id", 0)
			.withValue("mimetype", "vnd.android.cursor.item/name")
			.withValue("data2", contacts.getName()).build();
		operations.add(op2);
		
		ContentProviderOperation op3 = 
			ContentProviderOperation.newInsert(uri).withValueBackReference("raw_contact_id", 0)
			.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
			.withValue("data2", "2")
			.withValue("data1", contacts.getPhone()).build();
		operations.add(op3);
		
		ContentProviderOperation op4 = 
			ContentProviderOperation.newInsert(uri).withValueBackReference("raw_contact_id", 0)
			.withValue("mimetype", "vnd.android.cursor.item/email_v2")
			.withValue("data2", "2")
			.withValue("data1", contacts.getEmail()).build();
		operations.add(op4);
		
		contentResolver.applyBatch("com.android.contacts", operations);
		
	}
	
	//添加联系人
	public void saveContacts(MyContacts contacts){
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver contentResolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		//插入一条空数据获得raw_contacts 表 中id
		Uri insertUri = contentResolver.insert(uri, values);
		long id = ContentUris.parseId(insertUri); //_id
		//姓名
		uri = Uri.parse("content://com.android.contacts/data");
		values.put("raw_contact_id", id);
		values.put("mimetype", "vnd.android.cursor.item/name");
		values.put("data2", contacts.getName());
		contentResolver.insert(uri, values);
		//电话
		values.clear();
		values.put("raw_contact_id", id);
		values.put("mimetype", "vnd.android.cursor.item/phone_v2");
		values.put("data2", "2");
		values.put("data1", contacts.getPhone());
		contentResolver.insert(uri, values);
		//Email
		values.clear();
		values.put("raw_contact_id", id);
		values.put("mimetype", "vnd.android.cursor.item/email_v2");
		values.put("data2", "2");
		values.put("data1", contacts.getEmail());
		contentResolver.insert(uri, values);
	}
	
	public List<MyContacts> getContacts(){
		List<MyContacts> contacts = new ArrayList<MyContacts>();
		Uri uri = Uri.parse("content://com.android.contacts/contacts/");
		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(uri, new String[]{"_id"}, null, null, null);
		while(cursor.moveToNext()){
			String name = "";
			String phone = "";
			String email = "";
			Uri uri2 = Uri.parse("content://com.android.contacts/contacts/"+
					cursor.getString(cursor.getColumnIndex("_id"))+"/data");
			Cursor cursor2 = contentResolver.query(uri2, new String[]{"mimetype","data1"}, 
													null, null, "mimetype");
			while(cursor2.moveToNext()){
				if("vnd.android.cursor.item/name".equals(cursor2.getString(cursor2.getColumnIndex("mimetype"))))
					name = cursor2.getString(cursor2.getColumnIndex("data1"));
				if("vnd.android.cursor.item/phone_v2".equals(cursor2.getString(cursor2.getColumnIndex("mimetype"))))
					phone = cursor2.getString(cursor2.getColumnIndex("data1"));
				if("vnd.android.cursor.item/email_v2".equals(cursor2.getString(cursor2.getColumnIndex("mimetype"))))
					email = cursor2.getString(cursor2.getColumnIndex("data1"));
			}
			MyContacts contact = new MyContacts(name,phone,email);
			contacts.add(contact);
		}
		return contacts;
	}
}
